/*
 * Copyright 2011 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.gaellalire.jzip.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import fr.gaellalire.jzip.model.BuildState;
import fr.gaellalire.jzip.model.BuildStateListener;

/**
 *
 * @author Gael Lalire
 */
public class StatePanel extends JPanel implements BuildStateListener {

    private static final long serialVersionUID = -7388201093071823345L;

    private JLabel label;

    private JButton button;

    public StatePanel(final JZipPanel jZipPanel) {
        label = new JLabel("L'archive n'est pas modifiée");
        add(label);
        button = new JButton("Reconstruire l'archive");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent actionEvent) {
                try {
                    jZipPanel.rebuild();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        button.setVisible(false);
        add(button);
    }

    @Override
    public void buildStateChange(final BuildState buildState) {
        switch (buildState) {
        case IN_CREATION:
            button.setVisible(false);
            label.setText("L'archive est en cours de reconstruction");
            break;
        case MODIFIED:
            button.setVisible(true);
            label.setText("L'archive a été modifiée");
            break;
        case NOT_MODIFIED:
            button.setVisible(false);
            label.setText("L'archive n'est pas modifiée");
            break;
        default:
            break;
        }

    }
}
